package org.xzy.es.test;

import org.apache.http.HttpHost;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;

public class ESTest_Doc_Query {

    public static void main(String[] args) throws Exception{

        //创建ES客户端
        RestHighLevelClient esClient = new RestHighLevelClient(
                RestClient.builder(new HttpHost("127.0.0.1",9200))
        );

        //查询数据
        SearchRequest request = new SearchRequest();
        request.indices("user");

        //1.查询全量数据
        /*request.source(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()));

        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);

        SearchHits hits = response.getHits();

        System.out.println(response.getHits().getTotalHits());
        for(SearchHit hit:hits){
            System.out.println(hit.getSourceAsString());
        }*/

        //2.条件查询
        /*request.source(new SearchSourceBuilder().query(QueryBuilders.termQuery("age",30)));
        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);

        SearchHits hits = response.getHits();

        System.out.println(response.getHits().getTotalHits());
        for(SearchHit hit:hits){
            System.out.println(hit.getSourceAsString());
        }*/

        //3.分页查询
        /*SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
        builder.from(0).size(2);
        request.source(builder);
        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);

        SearchHits hits = response.getHits();

        System.out.println(response.getHits().getTotalHits());
        for(SearchHit hit:hits){
            System.out.println(hit.getSourceAsString());
        }*/

        //4.排序
        /*SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
        builder.sort("age",SortOrder.DESC);
        request.source(builder);
        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);

        SearchHits hits = response.getHits();

        System.out.println(response.getHits().getTotalHits());
        for(SearchHit hit:hits){
            System.out.println(hit.getSourceAsString());
        }*/

        //5.过滤字段
        SearchSourceBuilder builder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery());
        builder.sort("age",SortOrder.DESC);
        builder.fetchSource(new String[]{"name"},new String[]{});
        request.source(builder);
        SearchResponse response = esClient.search(request, RequestOptions.DEFAULT);

        SearchHits hits = response.getHits();

        System.out.println(response.getHits().getTotalHits());
        for(SearchHit hit:hits){
            System.out.println(hit.getSourceAsString());
        }

        //关闭ES客户端
        esClient.close();

    }
}
